package ro.gateway.aida.obj;

import java.util.Enumeration;
import java.util.Hashtable;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: AidaResource.java,v 1.1 2004/10/24 23:37:10 mihaipostelnicu Exp $
 */
public class AidaResource extends AIDAActivityObject
{
      private Hashtable s_descs;
      private Hashtable s_titles;
      private String link;
      private long id = -1;

      public AidaResource()
      {
            this.s_descs = new Hashtable();
            this.s_titles = new Hashtable();
            this.objectType = AIDAActivityObject.TYPE_RESOURCE;
      }

      public void setLink( String link )
      {
            this.link = link;
      }

      public String getLink()
      {
            return link;
      }

      public long getId()
      {
            return id;
      }

      public void setId( long id )
      {
            this.id = id;
      }

      public void setDesc( String lang, String desc )
      {
            if ( lang == null )
            {
                  return;
            }

            if ( desc != null )
            {
                  this.s_descs.put( lang, desc );
            }
            else
            {
                  this.s_descs.remove( lang );
            }
      }

      public void setTitle(String lang, String title) {
            if ( lang == null )
            {
                  return;
            }

            if ( title != null )
            {
                  this.s_titles.put( lang, title );
            }
            else
            {
                  this.s_titles.remove( lang );
            }
      }

      public String getTitle( String lang )
      {
            if ( lang == null )
            {
                  return null;
            }

            String result = ( String ) s_titles.get( lang );
            if ( result == null )
            {
                  String lng = "ro";
                  if ( s_titles.get( lng ) == null )
                  {
                        lng = "en";
                        if ( s_titles.get( lng ) == null )
                        {
                              Enumeration en = s_titles.keys();
                              if ( en.hasMoreElements() )
                              {
                                    lng = ( String ) s_titles.keys().nextElement();
                              }
                        }
                        if ( lng == null )
                        {
                              lng = "en";
                        }
                  }

                  String title = ( String ) s_titles.get( lng );
                  if ( title != null )
                  {
                        result = title + " (" + lng + ")";
                  }
            }
            return result;
      }

      public String getDesc( String lang )
      {
            if ( lang == null )
            {
                  return null;
            }

            String result = ( String ) s_descs.get( lang );
            if ( result == null )
            {
                  String lng = "ro";
                  if ( s_descs.get( lng ) == null )
                  {
                        lng = "en";
                        /*           if ( s_descs.get( lng ) == null ) {
                                        lng = ( String ) s_descs.keys().nextElement();
                                   }*/
                  }

                  String desc = ( String ) s_descs.get( lng );
                  if (desc != null) {
                        result = desc + " (" + lng + ")";
                  }

            }
            return result;
      }

      public void clearDescs()
      {
            this.s_descs.clear();
      }

      public String[] getAvailableLanguages()
      {
            if ( s_descs.size() < 1 )
                  return null;

            String[] result = new String[ s_descs.size() ];
            Enumeration en = s_descs.keys();
            int i = 0;
            while ( en.hasMoreElements() )
            {
                  result[ i++ ] = ( String ) en.nextElement();
            }

            return result;
      }

      public boolean equals( AidaResource resource )
      {
            if ( ( this.id == -1 ) && ( resource.id == -1 ) )
            {
                  return false;
            }
            return this.id == resource.getId();
      }
}
